# -*- coding: utf-8 -*-
# project:Uiautomator2_Hupu
# fileName:base
# author:Merlin
# dataTime:2020/7/2-22:21
# *******************************************#
import time

import uiautomator2 as u2
class Driver(object):
	def __init__(self, driver):
		self.driver = driver


	def click(self, element, timeout=None):
		return element.click(timeout=timeout)
	def click_by_point(self, x, y):
		'''
		坐标定位
		:param kwargs:
		:return:
		'''
		return self.driver.click(x, y)

	def long_click(self, element, time=10):
		return element.long_click()

	def double_click(self, x, y, duration=0.1):
		"""双击
		@:param duration 间隔
		"""
		self.driver.double_click(x, y, duration=duration)

	def get_element_by_xpath(self, location):
		return self.driver.xpath(location)

	def get_element_by_text(self, location, instance=0):
		return self.driver(text=location, instance=instance)

	def get_element_by_id(self, location, instance=0):
		return self.driver(resourceId=location, instance=instance)

	def get_element_by_className(self, location, instance=0):
		return self.driver(className=location, instance=instance)

	def get_element(self, id=None, text=None, className=None, xpath=None):
		'''
		综合定位，可以替代上面单个定位,存在xpath时优先使用xpath
		:param id:
		:param text:
		:param className:
		:param xpath:
		:return:
		'''
		if xpath:
			return self.driver.xpath(xpath)
		else:
			return self.driver(resourceId=id, text=text, className=className)




	def count_elements(self, element):
		'''
		计算elements的元素个数
		和 len(element) 相同
		:param element:
		:return:
		'''
		return element.count

	def get_child_elements_list(self, element, childLocation):
		'''
		返回一个元素的指定子元素的列表
		:param location: 父元素定位表达式
		:param childLocation:子元素定位表达式
		:return:子元素的列表
		'''
		elements = element.child(childLocation)
		eles = []
		item = iter(elements)
		for ele in item:
			eles.append(ele)
		return eles

	def get_left_element(self, element, location):
		'''
		获取element左侧元素
		:param element:
		:param location:
		:return:
		'''
		return element.left(location)

	def get_right_element(self, element, location):
		return element.right(location)

	def get_up_element(self, element, location):
		return element.up(location)

	def get_down_element(self, element, location):
		return element.down(location)


	def check_element(self, element, timeout=int(3)):
		'''
		检查特定元素是否存在，True or False
		:param element:
		:param timeout:
		:return:
		'''
		return element.exists(timeout=timeout)

	def get_element_screenshot(self, element, path='tmp.jog'):
		element.screenshot().save(path)
		return True

	def get_element_info(self, element):
		'''
		:param element:
		:return: 返回如下输出
		{
			u'contentDescription': u'',
			u'checked': False,
			u'scrollable': False,
			u'text': u'Settings',
			u'packageName': u'com.android.launcher',
			u'selected': False,
			u'enabled': True,
			u'bounds': {u'top': 385,u'right': 360,u'bottom': 585,u'left': 200},
			u'className': u'android.widget.TextView',
			u'focused': False,
			u'focusable': True,
			u'clickable': True,
			u'chileCount': 0,
			u'longClickable': True,
			u'visibleBounds': {u'top': 385,u'right': 360,u'bottom': 585,u'left': 200},
			u'checkable': False
		}
		'''
		return element.info

	def app_satrt(self, packageName, activity=None):
		return self.driver.app_start(packageName, activity=activity)

	def app_stop(self, packageName):
		return self.driver.app_stop(packageName)

	def app_clear(self, packageName):
		return self.driver.app_clear(packageName)

	def app_info(self, packageName):
		'''
		获取指定APP的信息
		:param packageName:
		:return:
		{
			'packageName': 'com.tencent.qgame', 
			'mainActivity': 'com.tencent.qgame.presentation.activity.SplashActivity', 
			'label': '企鹅电竞', 
			'versionName': '5.9.0.494', 
			'versionCode': 494, 
			'size': 84957530}
		'''
		return self.driver.app_info


	def device_info(self):
		'''
		获取设备info
		:return:
		'''
		return self.driver.info

	def window_size(self):
		'''
		获取窗口打开
		:return:
		'''
		return self.driver.window_size()

	def device_height(self):
		'''
		获取设备高度
		:return:
		'''
		return self.driver.info.get('displayHeight')

	def device_width(self):
		'''
		获取设备宽度
		:return:
		'''
		return self.driver.info.get("displayWidth")

	def screen_on(self):
		"""
		打开屏幕
		:return:
		"""
		self.driver.screen_on()

	def screen_off(self):
		"""
		关闭屏幕
		:return:
		"""
		self.driver.screen_off()

	def press(self, key):
		'''
		home, back, enter
		按下按键
		:return:
		'''
		return self.driver.press(key)
